<?php
defined('BASEPATH') or die('forbidden');
class Elitecopy_Controller Extends Controller
{
    private $rolename;
    private $m;    //数据库连接
    private $server_info;
    private $copoyid2name;

    function _init()
    {
        check_level(50,segment(0),segment(1));
        set_time_limit(0);

        //得到选择的区号
        $areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }

        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' and areanum='$areanum' ";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        // db数据库
        //$this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");
        $this->m = new MongoClient("mongodb://{$this->server_info['dbip']}");
        if (false == $this->m) {
            //内部有提示错误，在此就不提示了
            //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
        }

        $this->copoyid2name = array();
        $copysql =  "SELECT * FROM tt_copyid2name WHERE type='2' ";
        $this->db->query ( $copysql );
        while ( $n = $this->db->get_array () ) {
            $this->copoyid2name[(int)$n ['id']] = $n ['name'];
        }
    }

    function index()
    {
        $this->view->display("playercopy/elitecopy/index");
    }
    
    function flexigrid()
    {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }

        $selectinfo = array('_id' => 1, 'elite' => 1);
        $collection=$this->m->{$this->server_info['dbname']}->PlayerCopy;
        if($query!=''){
            $document=$collection->find($filter, $selectinfo);
        }else{
            $document=$collection->find(array(), $selectinfo);
        }

        date_default_timezone_set("PRC");
        $total = 0;
        foreach ($document as $key=>$value){
            $total += 1;
            $b['_id']=$value['_id'];
            $b['lastentercopytime']=get_datetime($value['elite']['lastcopytime']);
            $b['curid']=$value['elite']['curid'];
            $b['curname']=$this->copoyid2name[(int)$b['curid']];  //ID转换到名字
            $b['curchapter']=$value['elite']['curchapter'];
            $b['totalpasscopytimes']=$value['elite']['totalpasscopytimes'];
            $b['totalstarawardtimes']=$value['elite']['starboxawardtimes'];
            $b['totalstarnum']=$value['elite']['totalstarnum'];

            $data[]=array('id' => $b['_id'], 'cell' => $b);
        }

        $result = array(
                'page' => $page,
                'total' => $total,
                'rows' => $data
        );
        //$this->dbConnt->free_all();
        echo json_encode($result);
    }

    function del($id)
    {

    }
}
?>